package pro.cherish.knowledgeobjectstorage.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pro.cherish.knowledgeobjectstorage.core.StorageService;
import pro.cherish.knowledgeobjectstorage.fastdfs.client.FileResponseData;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 创建人：lys
 * @version 版本号：V1.0
 *     <p>***************************修订记录************************************ 2020-6-22 lys 创建该类功能。
 *     ********************************************************************** @ClassName
 *     类名：StorageController @Description 功能说明：
 *     <p>TODO **********************************************************************
 * @date 创建日期： 2020-6-22
 */
@Controller
@RequestMapping("/storage")
public class StorageController {

  @Autowired private StorageService storageService;
  /**
   * 只能上传图片，只上传文件到服务器，不会保存记录到数据库. <br>
   * 会检查文件格式是否正确，默认只能上传 ['png', 'gif', 'jpeg', 'jpg'] 几种类型.
   *
   * @param file
   * @return 返回文件路径等信息
   */
  @RequestMapping("/upload/image/sample")
  @ResponseBody
  public FileResponseData uploadImageSample(
      @RequestParam MultipartFile file) throws IOException {
    InputStream inputStream = file.getInputStream();
    String contentType = file.getContentType();
    contentType="application/octet-stream";
    int length = file.getBytes().length;
    String store = storageService.store(inputStream, length, contentType, file.getOriginalFilename());
    FileResponseData fileResponseData = new FileResponseData();
    System.out.println(store);
    fileResponseData.setMessage("上传成功");
    return fileResponseData;
  }
}
